package com.bwda.dsrs.filecenter.controller;

import com.alibaba.fastjson.JSONArray;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.common.enums.BusinessStatus;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.filecenter.domain.po.keyhistory.KeyHistoryPo;
import com.bwda.dsrs.filecenter.domain.po.registerkey.RegisterKeyPo;
import com.bwda.dsrs.filecenter.domain.po.sdklog.SdkLogPo;
import com.bwda.dsrs.filecenter.domain.vo.skeyInfo.SkeyInfoVo;
import com.bwda.dsrs.filecenter.service.ApplicationService;
import com.bwda.dsrs.filecenter.service.KeyHistoryService;
import com.bwda.dsrs.filecenter.service.RegisterKeyService;
import com.bwda.dsrs.filecenter.service.SdkLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 密钥信息
 *
 * @author bwda
 * @Date 2020-09-15
 */
@RestController
@RequestMapping("/b/sKeyInfo")
@Api(value = "/b/", tags = "密钥信息")
public class SKeyInfoController extends BaseController{
    @Autowired
    private RegisterKeyService registerKeyService;
    @Autowired
    private KeyHistoryService keyHistoryService;
    @Autowired
    private ApplicationService applicationService;

    @Autowired
    private SdkLogService sdkLogService;

    @ApiOperation(value = "查询密钥信息",notes = "查询密钥信息",httpMethod = "GET")
    @RequestMapping(value = "/getAllSKeyInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<List<RegisterKeyPo>> getAllSKeyInfo(@ApiParam(value = "应用编码code", required = true) @RequestParam String appCode) throws BusinessServiceException {
        //通过系统编码获取系统id
        Integer appId = applicationService.queryAppIdByAppCode(appCode);
        List<RegisterKeyPo> registerKeyPoList =  registerKeyService.queryRegisterKeyByAppId(appId);

        //历史秘钥数据封装
        List<SkeyInfoVo> historySKeyInfoList = new ArrayList<SkeyInfoVo>();
        if(registerKeyPoList!=null && registerKeyPoList.size()>0){

            for(RegisterKeyPo registerKeyPo:registerKeyPoList){
                //获取注册秘钥id
                Integer registerKeyId =  registerKeyPo.getRegisterKeyId();
                //场景编码
                String sceneCode = registerKeyPo.getSceneCode();
                //应用编码
                registerKeyPo.setAppCode(appCode);

                //最新版本
                registerKeyPo.setKeyVersion(registerKeyPo.getCurrentVersion());

                List<KeyHistoryPo> keyHistoryPoList= keyHistoryService.queryKeyHistoryByKeyId(registerKeyId);
                if(keyHistoryPoList!=null && keyHistoryPoList.size()>0){
                    //历史版本信息
                    for(KeyHistoryPo keyHistory:keyHistoryPoList){
                        //获取历史系统编码(历史和当前的数据不变)
                        String code =  keyHistory.getAppCode();
                        //原始秘钥
                        String secretKey =  keyHistory.getSecretKey();
                        //原始版本
                        String keyVersion = keyHistory.getKeyVersion();
                        //原始秘钥信息
                        SkeyInfoVo skeyInfoVo = new SkeyInfoVo();
                        //原始秘钥
                        skeyInfoVo.setSecretKey(secretKey);
                        skeyInfoVo.setSceneCode(sceneCode);
                        skeyInfoVo.setAppCode(code);
                        skeyInfoVo.setAlgorithm(registerKeyPo.getAlgorithm());
                        skeyInfoVo.setKeyVersion(keyVersion);
                        historySKeyInfoList.add(skeyInfoVo);
                    }
                    registerKeyPo.setHistorySKeyInfoList(historySKeyInfoList);
                }
            }
        }else{
            return  getFailResult("当前注册秘钥无数据");
        }
        return getSuccessResult(registerKeyPoList);
    }


    @ApiOperation(value = "SDK日志表",notes = "SDK日志表",httpMethod = "POST")
    @RequestMapping(value = "/addLogInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public String  addLogInfo(@RequestParam String logInfoList) {
        String result= BusinessStatus.SUCCESS.getCode();
        try {
            List<SdkLogPo> listAllSKey = JSONArray.parseArray(logInfoList, SdkLogPo.class);

            sdkLogService.insertList(listAllSKey);
        }catch (Exception e){
            result=BusinessStatus.ERROR.getCode();
        }

        return result;
    }
    /**
     * ClientAppVersionQueryForm转换为ClientAppVersionCondition
     *
     * @param form
     * @return
     */
   /* private ClientAppVersionCondition getConditionByQueryForm(ClientAppVersionQueryForm form) {
        ClientAppVersionCondition condition = CopyUtil.transfer(form, ClientAppVersionCondition.class);
        return condition;
    }*/

}